<template>
    <el-form :model="loginForm" :rules="fieldRules" ref="loginForm" label-position="left" label-width="0px"
             class="demo-ruleForm login-container">
    <span class="tool-bar">
      <!-- 主题切换 -->
      <theme-picker style="float:right;" class="theme-picker" :default="themeColor"
                    @onThemeChange="onThemeChange"></theme-picker>
    </span>
        <h2 class="title" style="padding-left:22px;">用户登录</h2>
        <el-form-item prop="account">
            <el-input type="text" maxlength="25" v-model.trim="loginForm.account" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input type="password" v-model.trim="loginForm.password" auto-complete="off" placeholder="密码" @keyup.enter.native="login" :loading="loading"></el-input>
        </el-form-item>
        <el-form-item style="width:100%;">
            <el-button type="primary" style="width:25%;" @click="reset">重 置</el-button>
            <el-button type="primary" style="width:25%;" @click="login" :loading="loading">登 录
            </el-button>
            <el-button type="primary" style="width:25%;" @click="register">注 册</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
import { mapState } from 'vuex'
import Cookies from 'js-cookie'
import ThemePicker from '@/components/ThemePicker'
export default {
  name: 'Login',
  components: {
    ThemePicker
  },
  data () {
    return {
      loading: false,
      loginForm: {
        account: '',
        password: ''
      },
      fieldRules: {
        account: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // 切换主题
    onThemeChange: function (themeColor) {
      this.$store.commit('setThemeColor', themeColor)
    },
    login: function () {
      this.loading = true
      if (this.loginForm.account === '' || this.loginForm.password === '') {
        this.loading = false
        this.$message.error('请填写用账号和密码')
        return
      }
      this.$api.login.login(this.loginForm).then((res) => {
        this.loading = false
        if (!res.isSuccess) {
          this.$message.error(res.msg)
        }
        Cookies.set('token', res.body.token)
        // 保存用户昵称到本地会话
        sessionStorage.setItem('user', res.body.petName)
        // 用户头像地址
        sessionStorage.setItem('headPic', res.body.headPicMiddle)
        // 用户登录方式
        sessionStorage.setItem('typeStr', res.body.typeStr)
        // 注册时间
        sessionStorage.setItem('registerTime', res.body.registerTime)
        // 用户唯一码
        sessionStorage.setItem('userUnique', res.body.userUnique)
        this.$router.push('/')
        this.loading = false
      }).catch((res) => {
        this.$message.error(res.msg)
        this.loading = true
      })
    },
    register: function () {
      this.$router.push('/register')
    },
    reset: function () {
      this.$refs.loginForm.resetFields()
    }
  },
  computed: {
    ...mapState({
      themeColor: state => state.app.themeColor
    })
  }
}
</script>

<style lang="scss" scoped>
    .login-container {
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 160px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
        .title {
            margin: 0px auto 30px auto;
            text-align: center;
            color: #505458;
        }
        .remember {
            margin: 0px 0px 35px 0px;
        }
    }
</style>
